package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.TravelComment;
import cn.wolfcode.luowowo.query.TravelCommentQuery;
import cn.wolfcode.luowowo.repository.TravelCommentRepository;
import cn.wolfcode.luowowo.service.ITravelCommentService;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
//@Transactional  暂时先别管事务
public class TravelCommentServiceImpl implements ITravelCommentService {
    @Autowired
    private IUserInfoService userInfoService;
    
    @Autowired
    private TravelCommentRepository repository;
    
    @Autowired
    private MongoTemplate template;
    
    
    
    @Override
    public TravelComment get(String id) {
        TravelComment travelComment = repository.findById(id).get();
        return travelComment;
    }
    
    //条件查询
    //page对象里面包括: 总页数 每页显示条数......
    public Page<TravelComment> query(TravelCommentQuery qo) {
        /*创建一个空的条件*/
        Query query = new Query();
        if (StringUtils.hasLength(qo.getTravelId())) {
            query.addCriteria(Criteria.where("strategyId").is(qo.getTravelId()));
        }
        
        /*工具类传进条件后 进行数据的排序与分页*/
        Page<TravelComment> query1 = DBHelper.query(template, TravelComment.class, query, qo.getPageable());
        //查询用户的信息
        return query1;
    }
    
    @Override
    public List<TravelComment> getCommentByTravel(String travelId) {
        return repository.findByTravelId(travelId);
    }
    
    /*查询全部的内容*/
    public List<TravelComment> list() {
        return repository.findAll();
    }
    
    
    /**
     * 删除方法
     *
     * @param id 删除的id
     */
    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }
    
    /**
     * 添加与保存的方法
     *
     * @param travelComment
     */
    @Override
    public void save(TravelComment travelComment) {
        
        //防止在controller层的user注入将id添加了,这里将id设置为空
        travelComment.setId(null);
        //创建的时间戳
        travelComment.setCreateTime(new Date());
        
        //判断如果在评论中带有别的评论的id,说明我是评论了别人,就根据他的id查询到评论的数据封装进去
        String id = travelComment.getRefComment().getId();
        if (StringUtils.hasLength(id)){
            TravelComment travelComment1 = this.get(id);
            //将上次评论的用户值传到评论集合中
            travelComment.setRefComment(travelComment1);
            //同时修改评论的状态,改为评论评论的评论
            travelComment.setType(TravelComment.TRAVLE_COMMENT_TYPE);
        }
        repository.save(travelComment);
        
    }
    
    /**
     * 点赞功能
     * @param cid   评论id
     * @param id    用户的id
     */
    @Override
    public void commentThumb(String cid, String id) {
        //大体思路就是 去这个评论的点赞集合中进行判断
            //获取这个评论的内容
        TravelComment comment = repository.findById(cid).get();
        // 如果在集合中存在用户的id 那么就点赞状态设置为-1 同时将用户的id从集合中删除
       /* if (comment.getThumbuplist().contains(id)){
            comment.setThumbupnum(comment.getThumbupnum()-1);
            comment.getThumbuplist().remove(id);
        }else {//如果不存在 就点赞状态设置为+1  同时将用户id加进集合中
            comment.setThumbupnum(comment.getThumbupnum()+1);
            //添加
            comment.getThumbuplist().add(id);
        }*/
        repository.save(comment);
    }
    
   
}
